package controllers

import (
	"baokaobang/datasourse"
	"baokaobang/models"
	"baokaobang/utils"
	"fmt"

	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/sessions"
	rc "github.com/rongcloud/server-sdk-go/v3/sdk"
)

type GroupSilence struct {
	Ctx iris.Context
}

func (c *GroupSilence) PostSilence() interface{} {
	channel := c.Ctx.PostValue("channel")
	session := sessions.Get(c.Ctx)
	userType := c.Ctx.PostValue("type")

	if channel == ""  {
		return utils.CommonRes(1000, fmt.Sprintf("获取参数有误"), "")
	}

	//连接数据库
	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库：%s", err.Error()), "")
	}

	//获取userId
	userId, _ := session.GetInt64(fmt.Sprintf("userId: %s", channel))

	userInfo := new(models.User)

	//通过userId获取collegeCode
	ok, err := db.Table("user").ID(userId).Get(userInfo)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("获取用户信息出错", err.Error()), "")
	}

	if ok {
		if userType == "2" {
			//创建融云对象  传入appkey、appsecret
			rongCloud := rc.NewRongCloud("x18ywvqfx4c1c", "Y3Vo93Qk1cC")
			if rongCloud == nil {
				return utils.CommonRes(1000, fmt.Sprintf("创建融云对象失败"), "")
			}

			err := rongCloud.GroupMuteAllMembersAdd([]string{userInfo.CollegeCode})
			if err != nil {
				return utils.CommonRes(1000, fmt.Sprintf("群组禁言失败"), "")
			}
		}
	}

	return utils.CommonRes(20000, fmt.Sprintf("全员禁言成功"), "")
}
